package xyz.o0o0o.demo.RedisDemo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import com.caucho.hessian.io.HessianOutput;

public class SpringRedis {

	public static void main(String[] args) throws IOException {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setUsePool(true);
		jedisConnectionFactory.afterPropertiesSet();
		jedisConnectionFactory.getConnection();

		RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
		redisTemplate.setConnectionFactory(jedisConnectionFactory);
		redisTemplate.setKeySerializer(redisTemplate.getStringSerializer());
		redisTemplate.setValueSerializer(redisTemplate.getStringSerializer());
		redisTemplate.afterPropertiesSet();

		// redisTemplate.opsForValue().set("aaa", "aaaaaaa".getBytes());
		// redisTemplate.opsForValue().get("aa");
		redisTemplate.opsForList().leftPush("aaa", "aaaaaaa");
		redisTemplate.opsForList().getOperations().expire("aaa", 1, TimeUnit.DAYS);

		RedisTemplate<String, byte[]> redisTemplate2 = new RedisTemplate<String, byte[]>();
		redisTemplate2.setConnectionFactory(jedisConnectionFactory);
		redisTemplate2.setKeySerializer(redisTemplate2.getStringSerializer());
		redisTemplate2.afterPropertiesSet();
		redisTemplate2.opsForList().leftPush("bbb", "bbbbbbb".getBytes());

		Date date = new Date();

		RedisTemplate<String, byte[]> redisTemplate3 = new RedisTemplate<String, byte[]>();
		redisTemplate3.setConnectionFactory(jedisConnectionFactory);
		redisTemplate3.setKeySerializer(redisTemplate3.getStringSerializer());
		redisTemplate3.setValueSerializer(new HessianRedisSerializer());
		redisTemplate3.afterPropertiesSet();
		ByteArrayOutputStream baos = null;
		HessianOutput output = null;
		baos = new ByteArrayOutputStream(1024);
		output = new HessianOutput(baos);
		output.startCall();
		output.writeObject(date);
		//output.completeCall();
		redisTemplate3.opsForList().leftPush("ccc", baos.toByteArray());

		RedisTemplate<String, Object> redisTemplate4 = new RedisTemplate<String, Object>();
		redisTemplate4.setConnectionFactory(jedisConnectionFactory);
		redisTemplate4.setKeySerializer(redisTemplate3.getStringSerializer());
		redisTemplate4.setValueSerializer(new HessianRedisSerializer());
		redisTemplate4.afterPropertiesSet();
		redisTemplate4.opsForList().leftPush("ddd", date);
	}

}
